<?php
/**
 * This file contains the class SubmitPersonModel which provides
 * most of the code requirements for the submit person page
 *
 * @copyright  2009 A Memory Tree
 * @license    A Memory Tree License
 * @version    $Id:$
 */
/**
 * This class provides the code for the Member page
 *
 * @copyright  2009 A Memory Tree
 * @license    A Memory Tree License
 * @version    Release: @package_version@
 */
class SubmitPersonModel
{
    /**
     * Access to the database methods
     *
     * @var SubmitPersonDB
     */
    private $_db;
    /**
     * Access to the View methods
     *
     * @var SubmitPersonView
     */
    private $_view;
    /**
     * Access to the Email methods
     *
     * @var EmailModel
     */
    private $_email;
    /**
     * Create a new instance of this class
     *
     * @param SubmitPersonDB $db
     * @param SubmitPersonView $view
     * @param EmailModel $email
     */
    public function __construct($db, $view, $email)
    {
        $this->_db = $db;
        $this->_view = $view;
        $this->_email = $email;
    }
    /**
     * Get the id number of a charity.
     *
     * If the charity doesn't exist, add a new charity and return that id number instead.
     *
     * @return string
     * @param mixed $charityName
     */
    function getCharityId($charityName) {
        $db = $this->_db;
        $id = $db->getCharityNameID($charityName);
        if (empty($id)) {
            $id = $db->insertCharity($charityName);
        }
        return $id;
    }
    /**
     * Check to see if a notification email should be sent
     * to the funeral home.
     *
     * @return bool
     * @param int $funeralHomeId
     */
    function notifyCheck($funeralHomeId)
    {
        $db = $this->_db;
        $notify = FALSE;
        $addListingNotify = $db->getAddListingNotify($funeralHomeId);
        if ($addListingNotify === 1) {
            $notify = TRUE;
        }
        return $notify;
    }
    /**
     * Send a notification to the funeral home about the added person
     *
     * @return void
     * @param EmailModel $email
     * @param int $funeralHomeId
     * @param int $personId
     * @param string $fullname
     */
    function sendNotification($funeralHomeId, $personId, $fullname)
    {
        $db = $this->_db;
        $view = $this->_view;
        $email = $this->_email;
        $funeralHomeEmail = $db->getFuneralHomeEmail($funeralHomeId);
        if (empty($funeralHomeEmail)) {
            $funeralHomeEmail = 'funeralmatters@amemorytree.co.nz';
        }
        $funeralHomeName = $db->getFuneralHomeName($funeralHomeId);
        $message = $view->notificationMessage($personId, $fullname);
        $email->setToEmail($funeralHomeEmail);
        $email->setToName($funeralHomeName);
        $email->setSubject('For the family and friends of '.$fullname.', a remembrance page has been opened');
        $email->setMessage($message);
        $email->send();
    }
    /**
     * Add a person
     *
     * @return int               The id number of the newly added person
     * @param mixed $surname
     * @param mixed $firstNames
     * @param mixed $formerName
     * @param mixed $neeName
     * @param mixed $alsoKnownAs
     * @param mixed $deathDate
     * @param mixed $birthDate
     * @param mixed $age
     * @param mixed $ageMeasure
     * @param mixed $cityId
     * @param mixed $formerCityId
     * @param mixed $regionId
     * @param mixed $countryId
     * @param mixed $charityName
     * @param mixed $funeralHomeId
     * @param mixed $otherInfo 
     */
    function addPerson($surname, $firstNames, $formerName, $neeName, $alsoKnownAs, $deathDate, $birthDate, $age, $ageMeasure, $cityId, $formerCityId, $regionId, $countryId, $charityName, $funeralHomeId, $otherInfo)
    {
        $db = $this->_db;
        $charityId = $this->getCharityId($charityName);
        $personId = $db->insertNewPerson($surname, $firstNames, $formerName, $neeName, $alsoKnownAs, $deathDate, $birthDate, $age, $ageMeasure, $cityId, $formerCityId, $regionId, $countryId, $charityId, $funeralHomeId, $otherInfo);
        return $personId;
    }
    /**
     * Send an email to the funeral home
     *
     * @param int $funeralHomeId
     * @param int $personId
     * @param mixed $firstNames
     * @param mixed $surname
     */
    function sendEmail($funeralHomeId, $personId, $firstNames, $surname)
    {
        if (empty($funeralHomeId)) {
            return;
        }
        $db = $this->_db;
        $email = $this->_email;
        $fullname = $db->escape($firstNames).' '.strtoupper($db->escape($surname));
        if ($this->notifyCheck($funeralHomeId) === TRUE) {
            $this->sendNotification($funeralHomeId, $personId, $fullname);
        }
    }
}
?>
